package com.oa.pm.controller;

import com.joneying.common.web.response.Resp;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.pm.dto.HandoverAddDTO;
import com.oa.pm.dto.HandoverAuditDTO;
import com.oa.pm.entity.Handover;
import com.oa.pm.service.IHandoverService;
import com.oa.pm.vo.HandoverDetailsVO;
import com.oa.pm.vo.HandoverListVO;
import com.oa.system.dto.HandoverListDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/handover")
@Api(description = "离职交接表模块")
public class HandoverController extends BaseController {

    private final IHandoverService handoverService;

    @Autowired
    public HandoverController(IHandoverService handoverService) {
        this.handoverService = handoverService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp<List<HandoverListVO>> findList(@ApiParam("列表查询对象") HandoverListDTO handoverListDTO) {
        return new Resp().success(handoverService.findHandoverList(handoverListDTO));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<HandoverDetailsVO> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(handoverService.findHandoverById(id));
    }


    /**
     * 新增
     *
     * @param handover Handover
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "离职交接表对象") @RequestBody(required = false) HandoverAddDTO handover) throws BusinessException, DataIsEmptyException {
        return handoverService.add(getLoginUserId(), handover);
    }

    /**
     * 根据Id修改
     *
     * @param handover Handover
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "离职交接表对象") @RequestBody(required = false) Handover handover) {
        if (handoverService.updateById(handover)) {
            return new Resp().success();
        } else {
            return new Resp().failure();
        }
    }

    @PutMapping("/audit")
    @CommonController
    @Description("离职交接审核")
    @ApiOperation(value = "离职交接审核")
    public Resp audit(@ApiParam(value = "审核对象") @RequestBody(required = false) HandoverAuditDTO handover) throws DataIsEmptyException {
        return handoverService.audit(handover);
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Handover handover = handoverService.selectById(id);
        if (handover != null && handover.getIsDeleted() == 2) {
            handover.setIsDeleted(1);
            if (handoverService.updateById(handover)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
